package com.jt.assistant.ui

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.bean.Param
import com.jt.assistant.custom_view.ItemEdit
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_waybill.*

/**
 * 运单修改
 */
class WaybillActivity : BaseActivity<HomeViewModel>() {
    override fun layoutId(): Int = R.layout.activity_waybill

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        ie_waybillType.mData.add("正向送仓")
        ie_waybillType.mData.add("逆向退货")
        ie_waybillType.mEdit.setText("正向送仓")

        if (!intent.getStringExtra("RETURNS_CODE").isNullOrEmpty()){
            ie_waybillJDCode.mEdit.setText(intent.getStringExtra("RETURNS_CODE").toString())
            ie_waybillType.setCurrentPosition(1)
            ie_waybillType.isEnabled = false
            ie_cartonNum.visibility = View.GONE
            ie_volume.mEdit.requestFocus()
        }
    }

    override fun initListener() {
        ie_waybillType.mEdit.setOnClickListener {
            ie_waybillType.showListDialog("请选择运单类型", this)
        }

        ie_waybillType.setOnItemClickListener(object : ItemEdit.OnClickLister {
            override fun onItemClick(position: Int) {
                if (position == 0) {
                    ie_cartonNum.visibility = View.VISIBLE
                } else {
                    ie_cartonNum.visibility = View.GONE
                }
            }
        })

        bt_commit.setOnClickListener {
            if (ie_waybillJDCode.mEdit.text.isNullOrEmpty()) {
                ToastUtils.showShort("请输入渠道订单号")
                return@setOnClickListener
            }
            if (ie_waybillType.mCurrentPosition == 0 && ie_cartonNum.mEdit.text.isNullOrEmpty()) {
                ToastUtils.showShort("请输入箱数")
                return@setOnClickListener
            }
            if (ie_volume.mEdit.text.isNullOrEmpty()) {
                ToastUtils.showShort("请输入体积")
                return@setOnClickListener
            }

            val params: Param = Param::class.java.newInstance()
            params.channelOrderNo = ie_waybillJDCode.mEdit.text.toString()
            params.volume = ie_volume.mEdit.text.toString()

            if (ie_waybillType.mCurrentPosition == 0) {//正向送仓
                params.cartonNum = ie_cartonNum.mEdit.text.toString()
                params.channelNo = "0"
                viewModel.modifyWaybill(params)

            } else {//逆向退货
                viewModel.modifyReturnsWaybill(params)
            }
        }

        viewModel.mModifyWaybill.observe(this, {
            if (it.isSuccess()) {
                speakString("修改成功")
                finish()
            }
        })

        viewModel.mModifyReturnsWaybill.observe(this, {
            if (it.isSuccess()) {
                speakString("修改成功")
                finish()
            }
        })

    }

}